package com.microbroadcast.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.microbroadcast.resource.Comment;
import com.microbroadcast.resource.UserBasic;

public class CommentPersistence {

	public static List<Comment> getCommentsByMessageID(long messageID) {
		ArrayList<Comment> result = new ArrayList<Comment>();
		try {
			Connection c = DBHelper.getConnection();
			String sql = "select * from comment where messageID = ?";
			PreparedStatement ps = c.prepareStatement(sql);
			ps.setLong(1, messageID);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				long commentID = rs.getLong("CommentID");
				String text = rs.getString("Text");
				Date createAt = rs.getDate("CreateAt");
				long userID = rs.getLong("UserID");
				long replyCommentID = rs.getLong("ReplyCommentID");
				UserBasic user = UserBasicPersistence.getUserBasic(userID);
				Comment com = new Comment(commentID, text, createAt, null,
						user, replyCommentID);
				result.add(com);
			}
			rs.close();
			ps.close();
			c.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return result;
		// TODO Auto-generated method stub
	}

}
